// Fill out your copyright notice in the Description page of Project Settings.


#include "Core/EnemyAIController.h"

#include "Core/EnemyZDCharacter.h"
#include "Navigation/PathFollowingComponent.h"



void AEnemyAIController::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	// UE_LOG(LogTemp, Warning, TEXT("next: %s, moving: %d, moveSuccess: %d"), *NextLocation.ToString(), IsMoving, IsMoveSuccess);
	StartMove();
}

void AEnemyAIController::BeginPlay()
{
	Super::BeginPlay();
}

void AEnemyAIController::OnPossess(APawn* InPawn)
{
	Super::OnPossess(InPawn);
	PrimaryActorTick.TickInterval = 2.0f;
	
	OwnerCharacter = Cast<AEnemyZDCharacter>(InPawn);
	StartMove();
}


void AEnemyAIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
	Super::OnMoveCompleted(RequestID, Result);
	IsMoveSuccess = Result.IsSuccess();
	IsMoving = false;
}

void AEnemyAIController::SetNextLocation(const FVector& Location)
{
	NextLocation = Location;	
}

void AEnemyAIController::StartMove()
{
	if (!IsMoving && OwnerCharacter)
	{
		IsMoving = true;
		if (IsMoveSuccess)
		{
			NextLocation = OwnerCharacter->GetNextLocation();
		}

		MoveToLocation(NextLocation);
	}
}

